/*
 * Copyright (c) 2017. 深圳一七网络科技. All rights reserved.
 */

package com.fenxiangle.yueding.feature.publish.presenter;

import android.text.TextUtils;

import com.amap.api.maps.MapView;
import com.amap.api.services.core.PoiItem;
import com.fenxiangle.yueding.entity.bo.AliPayBo;
import com.fenxiangle.yueding.entity.bo.MyAccountBo;
import com.fenxiangle.yueding.entity.bo.PageBo;
import com.fenxiangle.yueding.entity.bo.PayBo;
import com.fenxiangle.yueding.entity.bo.PublishBo;
import com.fenxiangle.yueding.entity.bo.PublishDemandBo;
import com.fenxiangle.yueding.feature.publish.contract.PayContract;
import com.fenxiangle.yueding.feature.publish.contract.PublishContract;
import com.fenxiangle.yueding.feature.publish.dependencies.mine.DaggerPublishPresenterComponent;
import com.fenxiangle.yueding.feature.publish.dependencies.pay.DaggerPayPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class PayPresenter implements PayContract.Presenter {

    private PayContract.View mView;


    @Inject

    PayContract.Model mModel;


    public PayPresenter(PayContract.View mView) {
        this.mView = mView;
        DaggerPayPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void getPayData(String type, int payStatus, String serialNumber, String transactionMount) {

        mModel.getPayData(new PayBo(type, payStatus, serialNumber, transactionMount))
                .compose(mView.bindToLife())
                .subscribe(new Observer<AliPayBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(AliPayBo aliPayBo) {
                        mView.dismissLoading();
                        if (payStatus == 0) {
                            mView.showAliPayData(aliPayBo.getAlipay_url());
                        } else if (payStatus == 1) {
                            mView.showWxPayData(aliPayBo.getWxpay_url());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void rechargeCredit(String type, int payStatus, String transactionMount) {
        if (Integer.valueOf(transactionMount)<100){
            mView.showError("最低充值100！");
            return;
        }

        mModel.rechargeCredit(new PayBo(type, payStatus, Integer.valueOf(transactionMount)))
                .compose(mView.bindToLife())
                .subscribe(new Observer<AliPayBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(AliPayBo aliPayBo) {
                        mView.dismissLoading();
                        if (payStatus == 0) {
                            mView.showAliPayData(aliPayBo.getAlipay_url());
                        } else if (payStatus == 1) {
                            mView.showWxPayData(aliPayBo.getWxpay_url());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getMyAccount() {
        mModel.getMyAccount().compose(mView.bindToLife())
                .subscribe(new Observer<MyAccountBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(MyAccountBo myAccountBo) {
                        mView.dismissLoading();
                        mView.showAccountSuccess(myAccountBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
